<?php

/**
 * 客服发送消息
 */
namespace bc\wx\message;

use bc\util\Util;
use bc\model\wx\ApiLogModel;


class Custom
{
	
	/**
	 * 
	 * @param array $data
	 * 
	 * "title": "Happy Day", 
	 * "description": "Is Really A Happy Day", 
	 * "url": "http://jscss.kdslife.com/club/html/images/icon/ico5.gif", 
	 * "picurl": "http://jscss.kdslife.com/club/html/images/icon/ico5.gif"
	 */
	public function send($openId, $accessToken, $data)
	{
		$arr = [];
		$arr['touser'] = $openId;
		$arr['msgtype'] = 'news';
		$arr['news']['articles'][] = $data;
		
		$params = Util::jsonEncode($arr);
		
		$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $accessToken;
		$rs = json_decode ( Util::httpPost( $url, $params ), true );
		
		$log = new ApiLogModel();
		$logData['result_status'] = $rs['errcode']?$rs['errcode']:0;
		$logData['url'] = $url;
		$logData['data'] = $params;
		$logData['result_data'] = json_encode($rs);
		$logData['create_time'] = time();
		$log->add($logData);
		return $rs;
	}
}

 